﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Inter.MianShi.Model;
using System.Data.SqlClient;
using System.Data;

namespace Inter.MianShi.DAL
{
    public class ResumeDal
    {
        // 连接数据库的字符串
        string str = "Data Source=169.254.44.71;Initial Catalog=MianShiHuiZong;Persist Security Info=True;User ID=sa";

        /// <summary>
        /// 简历信息的显示
        /// </summary>
        /// <returns></returns>
        public List<Resume> showresume()
        {
            SqlConnection con = new SqlConnection(str);
            con.Open();
            string sql = "select * from Resume";
            SqlCommand comm = new SqlCommand(sql, con);
            SqlDataAdapter adpter = new SqlDataAdapter(comm);
            DataTable table = new DataTable();
            adpter.Fill(table);

            List<Resume> list = new List<Resume>();
            foreach (DataRow item in table.Rows)
            {
                Resume r = new Resume();
                r.ResumeId = Convert.ToInt32(item["ResumeId"]);
                r.ResumeArea = item["ResumeArea"].ToString();
                r.ResumeQiwang = item["ResumeQiwang"].ToString();
                r.ResumeQijob = item["ResumeQijob"].ToString();
                r.ResumeQiindustry = item["ResumeQiindustry"].ToString();
                r.ResumeQiselect = Convert.ToInt32(item["ResumeQiselect"]);
                r.ResumeMuqian = item["ResumeMuqian"].ToString();
                r.ResumeSelf = item["ResumeSelf"].ToString();
                r.RXdate = Convert.ToDateTime(item["RXdate"]);
                r.RXrsetting = item["RXrsetting"].ToString();
                r.RXysetting = item["RXysetting"].ToString();
                r.RXktool = item["RXktool"].ToString();
                r.RXduty = item["RXduty"].ToString();
                r.RPdate = Convert.ToDateTime(item["RPdate"]);
                r.RPArea = item["RPArea"].ToString();
                r.RPcourse = item["RPcourse"].ToString();
                r.RPdescribe = item["RPdescribe"].ToString();
                r.RYlanguage = item["RYlanguage"].ToString();
                r.RPcourse = item["Rinterest"].ToString();
                list.Add(r);
            }
            con.Close();
            return list;
        }

        /// <summary>
        /// 简历信息的添加
        /// </summary>
        /// <param name="r"></param>
        /// <returns></returns>
        public bool Addresume(Resume r)
        {
            bool isdate = false;
            SqlConnection con = new SqlConnection(str);
            using (con)
            {
                con.Open();

                string sql = string.Format("insert into Resume (ResumeQiwang,ResumeQijob,ResumeQiindustry,ResumeQiselect,ResumeArea,ResumeMuqian,ResumeSelf,RXdate,RXrsetting,RXysetting,RXktool,RXduty,RPdate,RPArea,RPcourse,RPdescribe,RYlanguage,Rinterest) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}')", r.ResumeQiwang, r.ResumeQijob, r.ResumeQiindustry, r.ResumeQiselect, r.ResumeArea, r.ResumeMuqian, r.ResumeSelf, r.RXdate, r.RXrsetting, r.RXysetting, r.RXktool, r.RXduty, r.RPdate, r.RPArea, r.RPcourse, r.RPdescribe, r.RYlanguage, r.Rinterest);
                SqlCommand comm = new SqlCommand(sql, con);
                isdate = comm.ExecuteNonQuery() > 0;
            }
            return isdate;
        }

        /// <summary>
        /// 根据简历编号找到简历信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Resume getid(int id)
        {
            SqlConnection con = new SqlConnection(str);
            con.Open();
            string sql = string.Format("select * from resume where id={0}", id);
            SqlCommand com = new SqlCommand(sql, con);
            Resume res = new Resume();
            SqlDataReader reader = com.ExecuteReader();
            while (reader.Read())
            {
                res.ResumeId = Convert.ToInt32(reader["ResumeId"]);
                res.ResumeArea = reader["ResumeArea"].ToString();
                res.ResumeQiwang = reader["ResumeQiwang"].ToString();
                res.ResumeQijob = reader["ResumeQijob"].ToString();
                res.ResumeQiindustry = reader["ResumeQiindustry"].ToString();
                res.ResumeQiselect = Convert.ToInt32(reader["ResumeQiselect"]);
                res.ResumeMuqian = reader["ResumeMuqian"].ToString();
                res.ResumeSelf = reader["ResumeSelf"].ToString();
                res.RXdate = Convert.ToDateTime(reader["RXdate"]);
                res.RXrsetting = reader["RXrsetting"].ToString();
                res.RXysetting = reader["RXysetting"].ToString();
                res.RXktool = reader["RXktool"].ToString();
                res.RXduty = reader["RXduty"].ToString();
                res.RPdate = Convert.ToDateTime(reader["RPdate"]);
                res.RPArea = reader["RPArea"].ToString();
                res.RPcourse = reader["RPcourse"].ToString();
                res.RPdescribe = reader["RPdescribe"].ToString();
                res.RYlanguage = reader["RYlanguage"].ToString();
                res.RPcourse = reader["Rinterest"].ToString();
            }
            con.Close();
            return res;
        }

        /// <summary>
        /// 简历信息的修改
        /// </summary>
        /// <param name="re"></param>
        /// <returns></returns>
        public bool Updresume(Resume re)
        {
            bool isdate = false;
            SqlConnection con = new SqlConnection(str);
            using (con)
            {
                con.Open();
                string sql = string.Format("update Resume set (ResumeQiwang={0},ResumeQijob={1},ResumeQiindustry={2},ResumeQiselect={3},ResumeArea={4},ResumeMuqian={5},ResumeSelf={6},RXdate={7},RXrsetting={8},RXysetting={9},RXktool={10},RXduty={11},RPdate={12},RPArea={13},RPcourse={14},RPdescribe={15},RYlanguage={16},Rinterest={17}) where ResumeId={18}", re.ResumeQiwang, re.ResumeQijob, re.ResumeQiindustry, re.ResumeQiselect, re.ResumeArea, re.ResumeMuqian, re.ResumeSelf, re.RXdate, re.RXrsetting, re.RXysetting, re.RXktool, re.RXduty, re.RPdate, re.RPArea, re.RPcourse, re.RPdescribe, re.RYlanguage, re.Rinterest, re.ResumeId);
                SqlCommand comm = new SqlCommand(sql, con);
                isdate = comm.ExecuteNonQuery() > 0;
            }
            return isdate;
        }

        /// <summary>
        /// 简历信息的删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delresume(int id)
        {
            SqlConnection con = new SqlConnection(str);
            con.Open();
            string sql = "delete from Resume where ResumeId=" + id;
            SqlCommand com = new SqlCommand(sql, con);
            int count = com.ExecuteNonQuery();
            con.Close();
            return count > 0;
        }
    }
}
